
T = 1;

market_code = 'VNINDEX';
number_of_stocks = 20;
stock_codes = []; %{'BBT','TRI','NVN','VIS','SAV','PAC','TDH','TTF','VPH'}; 
porfolio_weights = [];

alpha = 0.05;
V0 = 1;

options = {'OLS'; 'Med Robust'; 'Trim Robust'; 'Weight Robust'; 'Shrinkage'; ...
          'Bayes'; ...
          'Smoothing'; ...
          'Kalman Filter';
          'Tobit';
          'Truncated'; 'GMM'};

% options = {'OLS'};

% temp = zeros(length(options), 1);
% TotalPayoff1 = temp;
% FailureRate = temp;
% 
% Beta_All = temp;
% Var_Up_All = temp;
% Var_Down_All = temp;
% 
% beta_all = temp;
% var_up_all = temp;
% var_down_all = temp;
% 
% TotalPayoff2 = temp;


for t = 1:T      
    t
%     [portfolio_codes porfolio_weights portfolio_date portfolio_close_price portfolio_volume] ...
%         = getPortfolio(market_code, number_of_stocks, stock_codes, porfolio_weights);
%     [portfolio_codes porfolio_weights portfolio_date portfolio_close_price portfolio_volume] = loadPortfolio(market_code, number_of_stocks, t);
    
    load(strcat('data\dtPortfolio_HOSE_20_',num2str(t),'.mat'));
    
    portfolio_codes = portfolio_codes_t;
    porfolio_weights = porfolio_weights_t;
    portfolio_date = portfolio_date_t;
    Stock_Price = portfolio_close_price_t(:, 2:end);
    Index_Price = portfolio_close_price_t(:, 1);
    
    
    %get return of Stocks and Index
    rPortfolio = getReturn(Stock_Price);
    rIndex = getReturn(Index_Price);

%     rPortfolio(find(abs(rPortfolio)>0.051)) = 0.0501;
%     max(rPortfolio)
%     var_quant(t) = quantile(rPortfolio*porfolio_weights,0.05);
    
    j=0;
    
    for i=1:length(options)
%         options{i}
        [totalPayoff1(t,i) Buy1 Sell1 totalPayoff2(t,i) Buy2 Sell2 beta VaRup VaRdown failure_rate(t,i)] ...
            = trading(Stock_Price, Index_Price, rPortfolio, rIndex, options{i},...
                       porfolio_weights, alpha, V0);
        if (length(beta) == 1)
            %j = j+1;
            beta_all(t,i) = beta;
            var_up_all(t,i) = VaRup;
            var_down_all(t,i) = VaRdown;
        else
            beta_multi = beta;
            var_down_multi = VaRdown;
            var_up_multi = VaRup;
            beta_all(t,i) = mean(beta_multi);
            var_down_all(t,i) = mean(var_down_multi);
            var_up_all(t,i) = mean(var_up_multi);
        end
        
    end
    
%     beta_all(7:8) =  [mean(beta_smoothing) mean(beta_kalman)];
%     var_down_all(7:8) = [mean(var_smoothing) mean(var_kalman)];
    
    
    %Beta_Smoothing(t) = mean(beta_smoothing);
    %Var_Smoothing(t) = mean(var_smoothing);
    %Beta_Kalman(t) = mean(beta_kalman);
    %Var_Kalman(t) = mean(var_kalman);
    
    
end
    
TotalPayoff1 = mean(totalPayoff1,1)
TotalPayoff2 = mean(totalPayoff2,1)

FailureRate = mean(failure_rate,1)
Beta_All = mean(beta_all,1);
Var_Up_All = mean(var_up_all,1);
Var_Down_All = mean(var_down_all,1);

% plot(var_quant)
% hold on
% plot(var_down_all(:,1),'r')
% % hold on
% % plot(var_down_all(:,2),'g')
% hold off

% Beta_Smoothing = mean(Beta_Smoothing);
% Var_Smoothing = mean(Var_Smoothing);
% Beta_Kalman = mean(Beta_Kalman);
% Var_Kalman = mean(Var_Kalman);

% Beta_All = [Beta_All Beta_Smoothing Beta_Kalman];
% Var_Down_All = [Var_Down_All Var_Smoothing Var_Kalman];

% 
% 
figure('name','Total Payoff - VaR stop-loss trading Strategy 1 & 2');
plot(TotalPayoff1,'g','LineWidth',2);
hold on
plot(TotalPayoff2,'b','LineWidth',2);
legend('return(%) - strategy 1','return(%) - strategy 2','Location','NorthWestOutside')
title('Total Payoff - VaR stop-loss trading Strategy 1 & 2',... 
  'FontWeight','bold')
xlabel('Beta estimation methods','FontWeight','bold')
ylabel('Return(%)','FontWeight','bold')
set(gca,'Xtick',1:40,'XTickLabel',{'OLS','Med','Trim','Wei','Shri','Bay','Smo','Kal','Tob','Tru','GMM'});

% figure('name','Beta Estimation');
% plot(Beta_All,'g','LineWidth',2);
% legend('Beta','Location','NorthWest')
% title('Beta Estimation',... 
%   'FontWeight','bold')
% xlabel('Beta estimation methods','FontWeight','bold')
% ylabel('Beta','FontWeight','bold')
% set(gca,'Xtick',1:40,'XTickLabel',{'OLS','Med','Trim','Wei','Shri','Bay','Smo','Kal','Tob','Tru','GMM'});
% 
% 
% figure('name','VaR Backtest - Failure Rate');
% plot(FailureRate,'g','LineWidth',2);
% yPos = 0.05;
% hold on
% plot(get(gca,'xlim'), [yPos yPos],'LineWidth',2); % Adapts to x limits of current axes
% legend('Failure rate(%)','Location','NorthWest')
% title('VaR Backtest - Failure Rate',... 
%   'FontWeight','bold')
% xlabel('Beta estimation methods','FontWeight','bold')
% ylabel('Failure rate(%)','FontWeight','bold')
% set(gca,'Xtick',1:40,'XTickLabel',{'OLS','Med','Trim','Wei','Shri','Bay','Smo','Kal','Tob','Tru','GMM'});
% 
% 
% figure('name','VaR All')
% plot(Var_Down_All,'b','LineWidth',2);
% legend('Var 5(%)','Location','NorthWest')
% title('VaR 5%',... 
%   'FontWeight','bold')
% xlabel('Beta estimation methods','FontWeight','bold')
% ylabel('Return(%)','FontWeight','bold')
% set(gca,'Xtick',1:40,'XTickLabel',{'OLS','Med','Trim','Wei','Shri','Bay','Smo', 'Kal','Tob','Tru','GMM'});
% 
% figure('name','VaR All')
% plot(Var_Up_All,'r','LineWidth',2);
% legend('Var 95(%)','Location','NorthWest')
% title('VaR 95%',... 
%   'FontWeight','bold')
% xlabel('Beta estimation methods','FontWeight','bold')
% ylabel('Return(%)','FontWeight','bold')
% set(gca,'Xtick',1:40,'XTickLabel',{'OLS','Med','Trim','Wei','Shri','Bay','Smo', 'Kal','Tob','Tru','GMM'});
% 
% 
% hold off;

% count=0;
% for i=2:length(portfolio_date)
%     temp = portfolio_date(i)-portfolio_date(i-1);
%     if ~(temp == 1) && ~(temp == 3)
%        count = count+1; 
%        i
%     end
% end

% x=0.5;
% for i=1:100
%     i
%     x=x+0.05;
% a(i) = BetaTruncatedLikelihood([x,0.00009], Hn(:, 1), Hn(:, 2), 0.05);
% end
% 
% plot(a)
% 
% clear a
% size(a)
